package com.quasar.hpatient.module.mine_password;

import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.quasar.hpatient.R;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.util.InputUtil;
import lib.quasar.widget.menu.NavigationView;
import lib.quasar.widget.timer.TimerView;

/**
 * description: 修改密码
 * created by kalu on 2018/5/17 13:37
 */
public class MinePasswordActivity extends BaseActivity<MinePasswordPresenter> implements MinePasswordView {

    public final static int RESULT_CODE = 1113;

    @Override
    public int initView() {
        return R.layout.activity_mine_password;
    }

    @Override
    public void initDataLocal() {

        // 发送验证码
        final TimerView timer = findViewById(R.id.mine_password_timer);
        final TextView getcode = findViewById(R.id.mine_password_getcode);
        getcode.setOnClickListener(v -> getPresenter().sendCode(MinePasswordActivity.this, timer, getcode));

        NavigationView menu = findViewById(R.id.mine_password_menu);
        menu.setOnNavigationChangeListener((isBack, isMenu) -> {
            if (isBack) onBackPressed();
            else {

                final TextView newPassword = findViewById(R.id.mine_password_new);
                final TextView surePassword = findViewById(R.id.mine_password_sure);

                if (getcode.getVisibility() != View.GONE) {
                    final TextView code = findViewById(R.id.mine_password_code);
                    getPresenter().forgetPassword(MinePasswordActivity.this, code, newPassword, surePassword);
                } else {
                    final TextView password1 = findViewById(R.id.mine_password_old);
                    getPresenter().modifyPassword(MinePasswordActivity.this, password1, newPassword, surePassword);
                }
            }
        });

        // 忘记密码
        final View forget = findViewById(R.id.mine_password_forget);
        final View codes = findViewById(R.id.mine_password_code_root);
        final View olds = findViewById(R.id.mine_password_old_root);
        forget.setOnClickListener(v -> {
            setVisibility(codes, View.VISIBLE);
            setVisibility(olds, View.GONE);
            setVisibility(forget, View.GONE);
        });

        // 显示隐藏密码
        final EditText input1 = findViewById(R.id.mine_password_old);
        final ImageView image1 = findViewById(R.id.mine_password_old_eye);
        image1.setOnClickListener(view -> {
            boolean ok = (input1.getTransformationMethod() == PasswordTransformationMethod.getInstance());
            image1.setImageDrawable(getResources().getDrawable(ok ? R.drawable.ic_comm_regist_password_on : R.drawable.ic_comm_regist_password_off));
            InputUtil.toogleKeybord();
        });
        final EditText input2 = findViewById(R.id.mine_password_new);
        final ImageView image2 = findViewById(R.id.mine_password_new_eye);
        image2.setOnClickListener(view -> {
            boolean ok = (input2.getTransformationMethod() == PasswordTransformationMethod.getInstance());
            image2.setImageDrawable(getResources().getDrawable(ok ? R.drawable.ic_comm_regist_password_on : R.drawable.ic_comm_regist_password_off));
            InputUtil.toogleKeybord();
        });
        final EditText input3 = findViewById(R.id.mine_password_sure);
        final ImageView image3 = findViewById(R.id.mine_password_sure_eye);
        image3.setOnClickListener(view -> {
            boolean ok = (input3.getTransformationMethod() == PasswordTransformationMethod.getInstance());
            image3.setImageDrawable(getResources().getDrawable(ok ? R.drawable.ic_comm_regist_password_on : R.drawable.ic_comm_regist_password_off));
            InputUtil.toogleKeybord();
        });
    }

    @Override
    public void initDataNet() {
    }

    @Override
    public void logout() {
        setResult(RESULT_CODE);
        onBackPressed();
    }
}